package brian.android.mvc.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import brian.android.mvc.annotation.ViewIdNotExistsException;
import brian.android.mvc.wiget.SelfListAdapter;
import brian.android.mvc.wiget.SimpleListAdapter;

@SuppressWarnings("unchecked")
public abstract class AbstractListActivity extends AbstractActivity implements OnItemClickListener, SimpleListAdapter {
	private List listItems = new ArrayList();
	
	@Override
	public void initAfterViewSet(Bundle savedInstanceState) {
		ListAdapter listAdapter = new SelfListAdapter(this, getListItemLayoutId(), getListItems());
		ListView listView = (ListView) findViewById(getListViewId());
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(this);
		List initListItems = getInitListItems();
		addListItemAll(initListItems);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Object listItem = getListItem(position);
		onItemClick(listItem, parent, view, position, id);
	}
	
	public List getListItems() {
		return listItems;
	}
	
	public Object getListItem(int location) {
		return listItems.get(location);
	}
	
	public void addListItemAll(List persistentNotes) {
		listItems.addAll(persistentNotes);
	}
	
	public void addListItem(Object listItem) {
		listItems.add(listItem);
	}

	@Override
	protected int getContentView() throws ViewIdNotExistsException {
		return getListLayoutId();
	}
	
	abstract public List getInitListItems();
	
	abstract protected void onItemClick(Object listItem, AdapterView<?> parent, View view, int position, long id);
	
	abstract protected int getListItemLayoutId();

	abstract protected int getListViewId();

	abstract protected int getListLayoutId();

}
